/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

import Model.Comentario;
import Model.Comentarios;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;

/**
 *
 * @author pedro
 */
public class ComentarioUtil {

    public  static  String urlServer=ConfUtil.url; 
    private String cod;

    public String getCod() {
        return cod;
    }

    public void setCod(String cod) {
        this.cod = cod;
    }

    public static List<Comentarios> getComentarios(String cod) {



        ArrayList<Comentarios> comentariosUsuario = new ArrayList();

        Gson gson = new Gson();
        try {

            URL url = new URL(urlServer+"comentario");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "text/plain");

            OutputStream os = conn.getOutputStream();
            os.write(cod.getBytes());
            os.flush();

            if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            Type collectionType = new TypeToken<List<Comentarios>>() {
            }.getType();
            while ((output = br.readLine()) != null) {
                comentariosUsuario = gson.fromJson(output, collectionType);
            }
            conn.disconnect();

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }
        return comentariosUsuario;

    }

    public static boolean sendComment(String cod_post, int cod_usuario, String comentario) {
        Gson gson = new Gson();


        Comentario cb = new Comentario();
        Comentario teste = new Comentario();

        cb.setCodPost(Integer.parseInt(cod_post));
        cb.setCodUsuario(cod_usuario);
        cb.setComentario(comentario);

        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        boolean loggedIn = false;
        try {
            URL url = new URL(urlServer+"comentarioSave");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");


            String json = gson.toJson(cb);
            
            System.out.println(json);

            teste = gson.fromJson(json, Comentario.class);



            OutputStream os = conn.getOutputStream();
            os.write(json.getBytes());
            os.flush();

            if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
            }
             msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Informação", "Comentário salvo com sucesso");
             FacesContext.getCurrentInstance().addMessage(null, msg);

            conn.disconnect();

        } catch (Exception ex) {
            Logger.getLogger(ComentarioUtil.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }

        return true;
    }
    public static void deletaComentario(Comentario cadastro) {
        Gson g = new Gson();
        String input = g.toJson(cadastro);
        try {

            RequestContext context = RequestContext.getCurrentInstance();
            FacesMessage msg = null;
            boolean loggedIn = false;

            URL url = new URL(urlServer+"deletaComentario");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");

            OutputStream os = conn.getOutputStream();
            os.write(input.getBytes());
            os.flush();

            if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
            }
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Informação", "Comentario deletado Com Sucesso");

            FacesContext.getCurrentInstance().addMessage(null, msg);
            context.addCallbackParam("loggedIn", loggedIn);

            conn.disconnect();

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }
    }
}
